﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Facebook;
using Sketchial.Business;

namespace Skecthial.UI.Services
{
    /// <summary>
    /// Summary description for FacebookLogin
    /// </summary>
    public class FacebookLogin : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {
        private LoginHelper loginHelper = new LoginHelper();
        public void ProcessRequest(HttpContext context)
        {

            var accessToken = context.Request["accessToken"];
            context.Session.Add("token", accessToken.ToString());
            context.Session["AccessToken"] = accessToken;
            var client = new FacebookClient(accessToken);
            dynamic result = client.Get("me", new { fields = "id,first_name,last_name,name,email,website" });
            string name = result.name;
            string id = result.id;
            HttpContext.Current.Session.Add("sessionID", id);

            //LoginMessage response = loginHelper.CheckIfUserExists(id);

            HttpCookie Cookie1 = new HttpCookie("TokenCookie");

            Cookie1.Values.Add("TOKEN", accessToken.ToString());
            Cookie1.Expires = DateTime.Now.AddMinutes(20);


            context.Response.Cookies.Add(Cookie1);

            context.Response.Redirect("http://127.0.0.1:3298/", false);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}